#!/bin/sh
#
# 		$Id$
#
# Copyright (c) 2007 Mark Heily <devel@heily.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

define_str() {
  id=$1
  if [ -n "$2" ] ; then 
      val=$2
  else 
      val=$3
      lc_id=`echo $id | tr 'A-Z' 'a-z'`
      eval "cf_${lc_id}=$val"
  fi
  echo "#define $id \"$val\"" >> config.h
  echo "$id=$val" >> config.mk
}

report() {
  printf "checking for $1... "
  if [ $2 -gt 0 ]
  then
    echo "yes"
  else
    echo "no"
  fi
}

# Check if a symbol is defined in the C library
check_symbol() {
for sym in $@
do
  count=`egrep -c "^_*${sym}$" config.sym`
  SYMBOL=`echo $sym | tr 'a-z' 'A-Z'`
  echo "#define HAVE_${SYMBOL} $count" >> config.h
  echo "HAVE_${SYMBOL}=$count" >> config.mk
  report $sym $count
done
}

# Check if a header file exists in /usr/include
check_header() {
for hdr in $@
do
  echo "#include <$hdr>" | gcc -x c -fsyntax-only - 2>/dev/null
  if [ $? -eq 0 ] ; then
      result=1
  else
      result=0
  fi

  HEADER=`echo $hdr | tr 'a-z' 'A-Z' | tr '.' '_'`
  echo "#define HAVE_${HEADER} $result" >> config.h
  echo "HAVE_${HEADER}=$result" >> config.mk
  report $hdr $result
done
}

####------------------------- MAIN() ------------------------------####

printf "# Automatically generated -- do not edit\n\n" > config.mk

printf "/* Automatically generated by ./configure -- do not edit */
#ifndef _CONFIG_H
#define _CONFIG_H

" > config.h


for arg in $*
do
   arg=`echo $arg | sed 's/^--//' | tr '-' '_'`
   id=`echo $arg | awk -F= '{ print $1 }' | tr 'a-z' 'A-Z'`
   val=`echo $arg | awk -F= '{ print $2 }'`
   test -z "$val" && val=1
   echo "$id"="$val" >> config.mk
   eval "cf_$id=$val"
done

# Generate a symbol table from the standard C library
# Note that this fails in some cases:
#   1. When the symbol is prefixed somehow. Example is
#      asprintf(3) in glibc; the symbol name is __asprintf
#   1. When a macro is used to emulate the symbol
#
# A better check would be to compile a program like this:
#          printf "#include <unistd.h>\n#include <stdlib.h>\nint main() { exit(1); }\n" | gcc -x c -fsyntax-only - && echo hi
#
nm /usr/lib/libc.a 2>/dev/null | grep 'T ' |sed 's/.* T //' > config.sym

# Get interesting variables from the Makefile
#
if [ ! -f config.var ] ; then
  make config.var >/dev/null
  chmod 755 config.var
fi
. ./config.var
rm config.var

# If PACKAGE is not provided, use the name of the current directory
# (without any version information)
if [ "$PACKAGE" != "" ]
then
  define_str PACKAGE $PACKAGE
else 
  define_str PACKAGE `pwd | sed 's+.*/++; s/-[0-9].*//'`
fi

if [ "$VERSION" != "" ]
then
  define_str VERSION $VERSION
else
  define_str VERSION 0.1
fi

for id in CC CFLAGS LDFLAGS LIBS CPPFLAGS
do
	eval "val=\$$id"
	test -z "$val" || echo "$id=$val" >> config.mk
done

for symbol in $TEST_SYMBOLS
do
  check_symbol "$symbol"
done

for header in $TEST_HEADERS
do
  check_header "$header"
done

if [ -z "$cf_prefix"]
then
  cf_prefix="/usr/local"
fi

define_str "PREFIX" "$cf_prefix" "/usr/local"
define_str "BINDIR" "$cf_bindir" "$cf_prefix/bin"
define_str "SBINDIR" "$cf_sbindir" "$cf_prefix/sbin"
define_str "LIBDIR" "$cf_libdir" "$cf_prefix/lib"
define_str "INCLUDEDIR" "$cf_includedir" "$cf_prefix/include"
define_str "DATADIR" "$cf_datadir" "$cf_prefix/share"
define_str "LOCALSTATEDIR" "$cf_localstatedir" "/var"
define_str "MANDIR" "$cf_mandir" "$cf_prefix/share/man"

# Create 'PKG' prefixed variables by appending the package name.
#
define_str "PKGDATADIR" "$cf_pkgdatadir" "$cf_prefix/share/$PACKAGE"
define_str "PKGLOCALSTATEDIR" "$cf_pkglocalstatedir" "$cf_localstatedir/$PACKAGE"

printf "\n#endif /* _CONFIG_H */\n" >> config.h

rm config.sym

exit 0
